//
//  MDXPGApplianceApi.h
//  MideaWorld_Zizhu
//
//  Created by Minus on 4/23/15.
//  Copyright (c) 2015 Minus. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <MideaHomeSDK/MideaHomeSDK.h>
#import <JreEmulation.h>

#import "User.h"
#import "UserInfo.h"
#import "Family.h"
#import "FamilyInfo.h"
#import "ApplianceInfo.h"
#import "VersionInfo.h"
#import "DeviceAppoint.h"
#import "CurveSleep.h"
#import "Elec.h"
#import "QueryElecCell.h"
#import "CheckDetails.h"
#import "AcCheckItemDetails.h"
#import "MessageInfo.h"
#import "AppInfo.h"
#import "AirConditionerFuncs.h"
#import "DeviceStatus.h"
#import "DeHumidification.h"
#import "Humidification.h"
#import "DeHumiFunctions.h"
#import "HumiFunctions.h"

#import "BaseMessage.h"
#import "BaseAPI.h"
#import "AirConditionerFuncs.h"

#ifdef __cplusplus
extern "C" {
#endif

extern UIImage *clipImageToSquare(UIImage *image);

#ifdef __cplusplus
}
#endif

@interface MDXPGApplianceApi : BaseAPI

@property (nonatomic, assign) NSInteger elecTotalDay;

+ (instancetype)getInstance;


+ (MDDeviceManager *)deviceManager;

#pragma mark - 二维码

/**
 *  @brief  检查二维码是否合法
 *  @param  qrcode  扫描出来的二维码串
 *  @return
 */
- (BOOL)isQRCodeValid:(NSString *)qrcode;

/**
 *  @brief  从二维码获取目标设备SSID
 *  @param  qrcode  扫描出来的二维码串
 *  @return 返回目标设备的SSID
 */
- (NSString *)getSSIDfromQRCode:(NSString *)qrcode;

/**
 *  @brief  从二维码获取目标设备SN
 *  @param  qrcode  扫描出来的二维码串
 *  @return 返回目标设备SN
 */
- (NSString *)getSNfromQRCode:(NSString *)qrcode;

/**
 *  @brief  获取用户当前连接的SSID
 */
- (NSString *)getCurrentSSID;

/**
 *  @brief  wifi密码长度是否合法
 *  @param  password    wifi密码
 *  @param  security
 *  @return 密码合法则返回YES
 */
- (BOOL)isValidWifiPassword:(NSString *)password security:(MDWifiSecurity)security;



/**
 *  说明
 *
 *  类 BaseMessage，包含错误码code以及错误信息message，
 *                  code=0表示成功，其他表示失败.
 */

#pragma mark - 用户

/**
 *  @brief  检测邮箱是否已注册。
 *
 *  用户注册前需要客户端自动检测邮箱是否已被注册。
 *
 *  @param  email   用户邮箱
 *
 *  返回
 *  emailStatus  邮箱状态，0：未注册，1：已注册未激活，2：已注册已激活，3：已注册禁用
 */
- (void)checkEmailAvailability:(NSString *)email
                    completion:(void(^)(BaseMessage *baseMessage, int emailStatus))completion;

/**
 *  @brief  用户注册（通过邮箱）
 *  @param  userName    用户邮箱
 *  @param  password    用户密码
 */
- (void)userRegister:(NSString *)userName
            password:(NSString *)password
          completion:(void(^)(BaseMessage *baseMessage))completion;

/**
 *  @brief  获取登陆ID。
 *
 *  用户登录前需要先获取登录ID，用于登录时加密敏感参数。
 *
 *  @param  email   用户邮箱
 *
 *  返回
 *  loginID  登录ID
 */
- (void)getLoginID:(NSString *)email
        completion:(void(^)(BaseMessage *baseMessage, NSString *loginID))completion;

/**
 *  @brief  用户登录(邮箱登录)
 *  @param  email       用户邮箱
 *  @param  password    用户密码
 *  @param  loginID     用户登录ID
 *
 *  返回
 *  user    类 User 的一个实例，包含sessionId, userId, nickname, accessToken等信息.
 *  用户登录成功后，需要将sessionId保存起来，后续一些操作需要使用到该sessionId
 */
- (void)userLogin:(NSString *)email
         password:(NSString *)password
          loginID:(NSString *)loginID
       completion:(void(^)(BaseMessage *baseMessage, User *user))completion;

/**
 *  @brief  用户注销
 */
- (void)userLogout:(void(^)(BaseMessage *baseMessage))completion;

/**
 *  @brief  获取用户详细信息
 *
 *  获取用户详细信息，包括用户昵称、性别、年龄等。
 *
 *  返回
 *  userInfo    类 UserInfo 的一个实例，包含email, mobile, sex, age, address等用户信息
 */
- (void)getUserInfo:(void(^)(BaseMessage *baseMessage, UserInfo *userInfo))completion;

/**
 *  @brief  用户修改密码
 *  @param  oldPassword     旧密码
 *  @param  newPassword     新密码
 */
- (void)updatePassword:(NSString *)oldPassword
           newPassword:(NSString *)newPassword
            completion:(void(^)(BaseMessage *baseMessage))completion;

/**
 *  @brief  重置密码（通过邮箱）
 *
 *  重置密码，平台会向用户邮箱发送重置密码连接。
 *
 *  @param  email   用户邮箱
 */
- (void)forgetPassword:(NSString *)email
            completion:(void(^)(BaseMessage *baseMessage))completion;

/**
 *  @brief  修改用户信息
 *  @param  nickName   用户昵称
 */
- (void)userModifyInfo:(NSString *)nickName
            completion:(void(^)(BaseMessage *baseMessage))completion;

/**
 *  @brief  头像上传
 *  @param  image     图片
 *
 *  返回
 *  urlString   图片在服务器中的地址
 */
- (void)userIcon:(UIImage *)image completion:(void(^)(BaseMessage *baseMessage, NSString *urlString))completion;


#pragma mark - 家庭

/**
 *  @brief  获取家庭列表
 *
 *  返回
 *  数组 familyInfos 的每一项是类 FamilyInfo 的一个实例
 */
- (void)getHomeList:(void(^)(BaseMessage *baseMessage, NSArray *familyInfos))completion;

/**
 *  @brief  创建家庭
 *
 *  登录之后先获取家庭列表，如果没有家庭则创建一个，有家庭则不用创建。
 *
 *  @param  name            家庭名
 *  @param  description     描述
 *  @param  address         地址
 *
 *  返回
 *  family  类 Family 的一个实例，包含家庭ID、家庭号number
 */
- (void)createFamily:(NSString *)name
         description:(NSString *)description
             address:(NSString *)address
          completion:(void(^)(BaseMessage *baseMessage, Family *family))completion;


#pragma mark - 设备

/**
 *  @brief  获取家庭下的设备列表
 *
 *  获取家庭所绑定的家电列表。
 *
 *  返回
 *  数组 applianceInfos 的每一项是类 ApplianceInfo 的一个实例
 */
- (void)getDeviceList:(void(^)(BaseMessage *baseMessage, NSArray *applianceInfos))completion;

/**
 *  @brief  家电应用数据透传
 *
 *  平台将控制家电报文透传给家电
 *
 *  @param  applianceID     家电设备ID
 *  @param  order           控制命令报文
 *
 *  返回
 *  reply   家电响应结果（报文转化为字符串，转化规则同控制报文转化规则相同）
 *  设备状态，可用于更新界面
 */
- (void)transparentCmd:(NSString *)applianceID
                 order:(IOSByteArray *)order
            completion:(void(^)(BaseMessage *baseMessage, NSString *reply, DeviceStatus *status))completion;

/**
 *  除湿 控制
 */
- (void)transparentCmd:(NSString *)applianceID
                 order:(IOSByteArray *)order
            completionForDehumidifier:(void(^)(BaseMessage *baseMessage, NSString *reply, DeHumidification *status))completion;

/**
 *  加湿 控制
 */
- (void)transparentCmd:(NSString *)applianceID
                 order:(IOSByteArray *)order completionForHumidifier:(void(^)(BaseMessage *baseMessage, NSString *reply, Humidification *status))completion;

/**
 *  @brief  查询功能和状态
 *  @params info    设备信息，包含设备ID、设备SN
 *
 *  返回
 *  空调状态和功能信息
 */
- (void)queryDeviceInfo:(ApplianceInfo *)info completion:(void(^)(BaseMessage *baseMessage, DeviceStatus *status, AirConditionerFuncs *funcs))completion;

/**
 *  @brief  删除家电
 *
 *  用户可以将家电从家庭中移除
 *
 *  @param  applianceID     家电ID
 */
- (void)deleteDevice:(NSString *)applianceID
          completion:(void(^)(BaseMessage *baseMessage))completion;

/**
 *  @brief  获取家电信息
 *  @param  userID
 *  @param  familyID
 *  @param  applianceID
 */
- (void)getDeviceInfo:(NSString *)applianceID
           completion:(void(^)(BaseMessage *baseMessage))completion;

/**
 *  @brief  修改家电信息
 *  @param  applianceID     设备ID
 *  @param  applianceName   设备名
 */
- (void)deviceModifyInfo:(NSString *)applianceID
           applianceName:(NSString *)applianceName
              completion:(void(^)(BaseMessage *baseMessage))completion;

#pragma mark 绑定设备

/**
 *  @brief  判断设备是否已进入配置模式
 */
- (BOOL)isDeviceReady;

/**
 *  @brief  连接设备，10秒超时失败
 *  @param  ssid    设备的SSID
 */
-(void)connectDevice:(NSString *)ssid
          completion:(void(^)(NSDictionary *dic, NSError *error))completion;

/**
 *  @brief  搜索家庭AP列表，10秒超时失败
 *
 *  返回
 *  wifi列表
 */
- (void)findWifiApList:(void(^)(NSArray *wifiApList, NSError *error))completion;

/**
 *  @brief  绑定设备到路由器，90秒超时
 *  @param homeSSID         家庭wifi的热点名称
 *  @param password         家庭wifi的密码
 *  @param security         家庭wifi加密方式 0=OPEN  1=WEP  2=WPA
 *  @param qrcodeSN         用户扫描二维码获取到的设备SN
 *  @param deviceName       用户自定义的设备命名
 *
 *  返回 NSDictionary  {"result":{"id":"17938288333"}}
 */
- (void)bindDevice:(NSString *)homeSSID
          password:(NSString *)password
          security:(MDWifiSecurity)security
          qrcodeSN:(NSString *)qrcodeSN
        deviceName:(NSString *)deviceName
        completion:(void(^)(NSDictionary *dic, NSError *error))completion;

#pragma mark - 更新

/**
 *  @brief  获取最新的更新包版本号
 *
 *  返回
 *  数组 versionInfos 的每一项是类 VersionInfo 的一个实例
 */
- (void)checkForUpdate:(void(^)(BaseMessage *baseMessage, NSArray *versionInfos))completion;

/**
 *  @brief  获取升级包信息
 *  @param  versionCode     升级包版本号
 */
- (void)getLastestAppInfo:(NSString *)versionCode
               completion:(void(^)(BaseMessage *baseMessage, AppInfo *appInfo))completion;

/**
 *  @brief  刷新用户session
 *
 *  刷新session和客户端语言
 *
 *  @param  sessionID   用户会话ID
 */
- (void)refreshSession:(void(^)(BaseMessage *baseMessage))completion;


#pragma mark - 电量

/**
 *  @brief  查询电量
 *  @param  applianceID     设备ID
 *  @param  type  1-周, 2-月, 3-年
 *  @param  year
 *  @param  month
 *  @param  day
 *
 *  返回
 *  电量
 */
- (void)queryElec:(NSString *)applianceID
             type:(int)type
             year:(int)year
            month:(int)month
             day:(int)day
       completion:(void(^)(BaseMessage *baseMessage, Elec *elec))completion;


#pragma mark - 睡眠曲线

/**
 *  @brief  获取睡眠曲线状态
 *  @param  applianceID     设备ID
 *  @param  mode            2-制冷 4-制热
 *
 *  返回
 *  curveSleeps     每个成员是CurveSleep的一个实例
 */
- (void)getSleepCurveStatus:(NSString *)applianceID
                       mode:(int)mode
                 completion:(void(^)(BaseMessage *baseMessage, NSArray *curveSleeps))completion;

/**
 *  @brief  更新睡眠曲线值
 *  @param  applianceID     设备ID
 *  @param  curveSleep
 */
- (void)updateSleepCurve:(NSString *)applianceID
              curveSleep:(CurveSleep *)curveSleep
              completion:(void(^)(BaseMessage *baseMessage))completion;

/**
 *  @brief  开启睡眠曲线
 *  @param  curveSleep
 */
- (void)startCurveSleep:(NSString *)applianceID
             curveSleep:(CurveSleep *)curveSleep
             completion:(void(^)(BaseMessage *baseMessage))completion;

/**
 *  @brief  关闭睡眠曲线
 *  @param  applianceID
 */
- (void)stopCurveSleep:(NSString *)applianceID
            completion:(void(^)(BaseMessage *baseMessage))completion;

/**
 *  @brief  添加睡眠曲线
 *  @param  curveSleep
 */
- (void)addCurveSleep:(CurveSleep *)curveSleep
           completion:(void(^)(BaseMessage *baseMessage))completion DEPRECATED_ATTRIBUTE;

/**
 *  @brief  删除睡眠曲线
 *  @param  curveSleep
 */
- (void)deleteCurveSleep:(CurveSleep *)curveSleep
           completion:(void(^)(BaseMessage *baseMessage))completion DEPRECATED_ATTRIBUTE;

                      
#pragma mark - 空调体检

/**
 *  @brief  开始体检
 *  @param  applianceID     设备ID
 *
 *  返回
 *  体检详细信息
 */
- (void)startACCheck:(NSString *)applianceID
          completion:(void(^)(BaseMessage *baseMessage, CheckDetails *checkDetails))completion;

/**
 *  @brief  获取体检信息
 *  @param  applianceID     设备ID
 *
 *  返回
 *  体检详细信息
 *  checkItemDetails    的每一项是类 AcCheckItemDetails 的实例
 */
- (void)getACCheckDetails:(NSString *)applianceID
               completion:(void(^)(BaseMessage *baseMessage, NSArray *checkItemDetails))completion;


#pragma mark - 预约
                          
/**
 *  @brief  添加预约
 *  @param  appoint
 *
 *  返回
 *  appointID   预约ID
 */
- (void)addDeviceAppoint:(DeviceAppoint *)appoint
              completion:(void(^)(BaseMessage *baseMessage, NSString *appointID))completion;


/**
 *  @brief  修改预约
 *  @param  appoint
 */
- (void)updateDeviceAppoint:(DeviceAppoint *)appoint
                 completion:(void(^)(BaseMessage *baseMessage))completion;

/**
 *  @brief  获取预约列表
 *  @param  applianceID     设备ID
 *
 *  返回
 *  预约列表
 */
- (void)getAppointList:(NSString *)applianceID
            completion:(void(^)(BaseMessage *baseMessage, NSArray *deviceAppoints))completion;

/**
 *  @brief  开启预约
 *  @param  appointID       预约ID
 */
- (void)startAppoint:(NSString *)appointID
          completion:(void(^)(BaseMessage *baseMessage))completion;

/**
 *  @brief  关闭预约
 *  @param  appointID       预约ID
 */
- (void)stopAppoint:(NSString *)appointID
         completion:(void(^)(BaseMessage *baseMessage))completion;


#pragma mark - T1温度推送设置

/**
 *  @brief  增加T1温度推送设置
 *
 *  设置了推送之后，服务器会在设定的时间推送室内温度到App
 *
 *  @param  applianceID     设备ID
 *  @param  time            推送时间，格式："HH:mm"，UTC时间
 *  @param  week            推送周，123 代表周一、二、三推送，四五六日不推送
 *  @param  repeat          重复，1-重复，0不重复
 */
- (void)addT1Temperature:(NSString *)applianceID
                    time:(NSString *)time
                    week:(NSString *)week
                  repeat:(int)repeat
              completion:(void(^)(BaseMessage *baseMessage))completion;

/**
 *  @brief  查询T1温度推送设置
 *  @param  applianceID     设备ID
 *
 *  返回
 *  onoff   YES-推送开启，NO-推送关闭
 */
- (void)queryT1Temperature:(NSString *)applianceID
                completion:(void(^)(BaseMessage *baseMessage, BOOL onoff))completion;

/**
 *  @brief  更新T1温度推送设置
 *  @param  applianceID     设备ID
 *  @param  time            推送时间，格式："09:15"
 *  @param  week            推送周，1110000 代表周一、二、三推送，四五六日不推送
 *  @param  repeat          重复，1-重复，0不重复
 */
- (void)updateT1Temperature:(NSString *)applianceID
                       time:(NSString *)time
                       week:(NSString *)week
                     repeat:(int)repeat
                 completion:(void(^)(BaseMessage *baseMessage))completion;

/**
 *  Add by Minus on 07/09/2015
 */

/**
 *  @brief  查询B5
 */
- (void)queryB5:(NSString *)applianceId completion:(void (^)(BaseMessage *baseMessage, AirConditionerFuncs *funcs))completion;

/**
 *  @brief  查询内风机运行时间
 */
- (void)queryFansRuntime:(NSString *)applianceId completion:(void(^)(BaseMessage *baseMessage, int time))completion;

/**
 *  @brief  清除电量数据
 */
- (void)clearPowerCal:(NSString *)applianceId completion:(void(^)(BaseMessage *baseMessage))completion;

/** 
 *  @brief  设置当前空调的功能
 *  @param  type    单冷 - AirConditionerFuncs_get_COLD_AC()
                    单热 - AirConditionerFuncs_get_HOT_AC()
                    冷热 - AirConditionerFuncs_get_COLDHOT_AC()
 */
- (AirConditionerFuncs *)setFunctions:(ApplianceInfo *)info type:(jint)type;

/**
 *  @brief  获取当前空调的功能
 */
- (AirConditionerFuncs *)getFunctions:(ApplianceInfo *)info;

/**
 *  @brief  重发激活邮件
 */
- (void)sendActivateEmail:(NSString *)email completion:(void(^)(BaseMessage *baseMessage))completion;

#pragma mark - 新增接口

/**
 *  除湿B5机型识别
 *
 *  @param applianceId 设备id
 *  @param completion
 */
- (void)queryB5ForDehumi:(NSString *)applianceId completion:(void(^)(BaseMessage *baseMessage, DeHumiFunctions *funcs))completion;

/**
 *  加湿B5机型识别
 *
 *  @param applianceId 设备id
 *  @param completion
 */
- (void)queryB5ForHumi:(NSString *)applianceId completion:(void(^)(BaseMessage *baseMessage, HumiFunctions *funcs))completion;

@end
